home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------------------------
- *
- * Simple Sample PowerTalk Application Framework
- *
- * ©1991-1993 Apple Computer
- *
- -------------------------------------------------------------------------------------*/
- /*
- * myevents.c -- main event loop and basic event handling
- *
- * change history:
- *
- * SJF 08/23/93 1.0f1 update to final headers, fix comments
- * SJF 04/21/93 1.0b2 update to b2
- * SJF 03/01/93 1.0b1 added digital signatures
- * SJF 02/09/93 1.0b1 update to b1
- * SJF 10/13/92 1.0d4 update to a11
- * SJF 09/09/92 1.0d3 update to a9
- * SJF 05/07/92 1.0d2 update to a6
- * SJF 11/06/91 1.0d1 initial coding
- *
- */
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __QUICKDRAW__
- #include <QuickDraw.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
-
- #ifndef __DESK__
- #include <Desk.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #include "const.h"
- #include "mytypes.h"
- #include "mymenus.h"
- #include "globals.h"
- #include "utils.h"
- #include "mystandardmail.h"
- #include "windowstuff.h"
- #include "commands.h"
- #include "aevt.h"
- #include "windutils.h"
- #include "digisig.h"
-
- #include "myevents.h"
-
- /* main event loop */
-
- void MainLoop(void)
- {
- EventRecord ev;
- Boolean gotEvent;
- Point mousePt;
-
- gCursorRgn = NewRgn();
-
- // note: even this is an AOCE sample, I still support single-finder since the application
- // runs without AOCE being present, just like a well-behaved app should
-
- while (!gDone) {
- if (gHasWaitNextEvent)
- gotEvent = WaitNextEvent(everyEvent,&ev,SleepTime(),gCursorRgn);
- else {
- gotEvent = GetNextEvent(everyEvent,&ev);
- SystemTask();
- }
-
- ProcessEvent(&ev); // process the event we got
-
- if (gotEvent) {
- GetMouse(&mousePt);
- LocalToGlobal(&mousePt);
- HandleFixCursor(mousePt,gCursorRgn);
- }
-
- if (gMenusDirty) {
- DrawMenuBar();
- gMenusDirty = false;
- }
- }
-
- DisposeRgn(gCursorRgn);
- }
-
-
- /* calculate how long to "wait" */
-
- long SleepTime(void)
- {
- if (gInBackground)
- return kSleepBackground;
- else
- return kSleepForeground;
- }
-
-
- /* set cursor shape depending on mouse location */
-
- void HandleFixCursor(Point where,RgnHandle theRgn)
- {
- WindowPtr window;
- unsigned long crsrData[2];
-
- if (!gInBackground) {
- window = MyFrontWindow();
-
- if (IsAppWindow(window)) {
- crsrData[0] = *((unsigned long *) &where);
- crsrData[1] = (unsigned long)theRgn;
- SendWindowMessage(window,kFixCursorMessage,crsrData);
- }
- else {
- // we don't want a stream of mouse moved events
- SetRectRgn(theRgn,-32767,-32767,32767,32767);
- SetCursor(&qd.arrow);
- }
- }
- }
-
-
- /* idle time processing */
-
- void HandleIdle(WindowPtr window)
- {
- if (IsAppWindow(window))
- SendWindowMessage(window,kIdleMessage,nil);
- }
-
-
- /* process an event gotten by MainLoop() */
-
- void ProcessEvent(EventRecord *ev)
- {
- void *returnResult;
- WindowPtr evWindow;
-
- if (ev->what==updateEvt)
- evWindow = (WindowPtr)(ev->message);
- else
- evWindow = MyFrontWindow();
-
- returnResult = SendWindowMessage(evWindow,kEventMessage,ev);
- if (returnResult!=nil)
- return;
-
- switch (ev->what) {
- case mouseDown:
- HandleMouseDowns(ev);
- break;
- case keyDown:
- case autoKey:
- HandleKeyDowns(ev);
- break;
- case updateEvt:
- HandleUpdates((WindowPtr)ev->message);
- break;
- case activateEvt:
- HandleActivates(ev);
- break;
- case osEvt:
- HandleSREvt(ev->where,ev->message);
- break;
- case kHighLevelEvent:
- DoHighLevelEvent(ev);
- break;
- case nullEvent:
- HandleIdle(MyFrontWindow());
- break;
- }
- }
-
-
- /* Handles suspend and resume events */
-
- void HandleSREvt(Point where,long message)
- {
- extern NMRec *gNotify;
- extern Boolean gInBackground;
- unsigned long whatMessage;
-
- whatMessage = message >> 24;
-
- if (whatMessage==suspendResumeMessage) {
- if ((message & 1) != 0) {
- gInBackground = false;
- SetCursor(&qd.arrow);
- if (MyFrontWindow()) {
- HiliteWindow(MyFrontWindow(),true);
- DoActivate(MyFrontWindow(),true);
- }
- }
- else if (MyFrontWindow()) {
- gInBackground = true;
- HiliteWindow(MyFrontWindow(),false);
- DoDeActivate(MyFrontWindow(),true);
- }
- }
- else if ((whatMessage&mouseMovedMessage)==mouseMovedMessage)
- HandleFixCursor(where,gCursorRgn);
- }
-
-
- /* Handles activate and deactivate events for a window */
-
- void HandleActivates(EventRecord *ev)
- {
- if ((ev->modifiers & activeFlag) != 0) {
- DoActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
- }
- else {
- DoDeActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
- }
- }
-
-
- /* Handles activate events for a window */
-
- void DoActivate(WindowPtr window,Boolean chFlag)
- {
- SendWindowMessage(window,kActivateMessage,&chFlag);
- }
-
-
- /* Handles deactivate events for a window */
-
- void DoDeActivate(WindowPtr window,Boolean chFlag)
- {
- SendWindowMessage(window,kDeactivateMessage,&chFlag);
- }
-
-
- /* handles update events for a window */
-
- void HandleUpdates(WindowPtr window)
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(window);
- BeginUpdate(window);
- EraseRect(&window->portRect);
- SendWindowMessage(window,kUpdateMessage,nil);
- EndUpdate(window);
- SetPort(savePort);
- }
-
-
- /* handles program keydowns */
-
- void HandleKeyDowns(EventRecord *ev)
- {
- short theChar;
- WindowPtr window;
-
- theChar = ev->message & charCodeMask;
- if ((ev->modifiers & cmdKey) != 0)
- DoMenuCommand(MenuKey(theChar));
- else if (IsAppWindow(window=MyFrontWindow()))
- SendWindowMessage(window,kKeyMessage,&theChar);
- }
-
-
- /* handles mouse down events for a window */
-
- void HandleMouseDowns(EventRecord *ev)
- {
- WindowPtr window;
- short part;
-
- part = FindWindow(ev->where,&window);
-
- switch (FindWindow(ev->where,&window)) {
- case inMenuBar:
- DoMenuCommand(MenuSelect(ev->where));
- break;
- case inSysWindow:
- SystemClick(ev,window);
- break;
- case inDrag:
- DoDrag(window,ev->where);
- break;
- case inGrow:
- DoGrow(window,ev->where);
- break;
- case inGoAway:
- if (TrackGoAway(window,ev->where)) {
- CommCloseWindow(window);
- }
- break;
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(window,ev->where,FindWindow(ev->where,&window)))
- DoZoom(window,FindWindow(ev->where,&window));
- break;
- case inContent:
- DoContentClick(window,ev);
- break;
- }
- }
-
-
- /* handles window dragging */
-
- void DoDrag(WindowPtr window,Point globMouse)
- {
- Rect dragRect = kWindowDragLimits;
-
- DragWindow(window,globMouse,&dragRect);
- SetPort(window);
- }
-
-
- /* handles window growing */
-
- void DoGrow(WindowPtr window,Point globMouse)
- {
- long newSize;
- Rect windLimits;
- Rect oldSize;
- GrafPtr tempPort;
- WInfoHndl infoHndl;
-
- if (!IsAppWindow(window))
- return;
-
- infoHndl = GetWindowInfo(window);
- windLimits = (**((**infoHndl).printRecord)).prInfo.rPage;
- windLimits.right += windLimits.left+kScrollBarWidth+1+(**infoHndl).leftIndent;
- windLimits.bottom += windLimits.top+kScrollBarWidth+1+(**infoHndl).topIndent;
- windLimits.left = 100;
- windLimits.top = 100;
-
- oldSize = window->portRect;
- if ((newSize = GrowWindow(window,globMouse,&windLimits)) != 0) {
- GetPort(&tempPort);
- SetPort(window);
- SizeWindow(window,LoWord(newSize),HiWord(newSize),true);
- InvalRect(&window->portRect);
- SendWindowMessage(window,kResizeMessage,&oldSize);
- SetPort(tempPort);
- }
- }
-
-
- /* handles window zooms */
-
- // note: this stuff is a little off: the stdState can't grow any bigger when it needs to
- // (when the user changes the page setup for example). I didn't spend the time to put in
- // a "real" zoom feature, because it doesn't show off any of the PowerTalk features
- //
- void DoZoom(WindowPtr window,short zoomDir)
- {
- GrafPtr savePort;
- WInfoPtr infoPtr;
- char hState;
- Rect oldSize;
- short newWidth,newHeight;
- WStateData **stateHndl;
- Rect userState,stdState;
-
- GetPort(&savePort);
- infoPtr = BeginWindowAccess(window,&hState);
- SetPort(window);
-
- oldSize = window->portRect;
- stateHndl = (WStateData **) ((WindowPeek)window)->dataHandle;
- userState = (**stateHndl).userState;
- stdState = (**stateHndl).stdState;
-
- ClipPageSize(&stdState,infoPtr,&newWidth,&newHeight);
- stdState.right = stdState.left+newWidth;
- stdState.bottom = stdState.top+newHeight;
- (**stateHndl).stdState = stdState;
-
- ZoomWindow(window,zoomDir,true);
- EraseRect(&window->portRect);
-
- InvalRect(&window->portRect);
- SendWindowMessage(window,kResizeMessage,&oldSize);
-
- EndWindowAccess(window,hState);
- SetPort(savePort);
- }
-
-
- /* handles a click in a window content region */
-
- void DoContentClick(WindowPtr window,EventRecord *ev)
- {
- short part;
- Point mousePos;
- ControlHandle hitControl;
- ControlHitMessage ctrlMessage;
-
- if (window != MyFrontWindow()) {
- SelectWindow(window);
- return;
- }
-
- mousePos = ev->where;
- SetPort(window);
- GlobalToLocal(&mousePos);
- part = FindControl(mousePos,window,&hitControl);
-
- if (hitControl) {
- ctrlMessage.part = part;
- ctrlMessage.control = hitControl;
- ctrlMessage.ev = ev;
- SendWindowMessage(window,kHitControlMessage,&ctrlMessage);
- }
- else
- SendWindowMessage(window,kClickMessage,ev);
- }
-
-
- /* handles menu commands */
-
- void DoMenuCommand(long mResult)
- {
- short selItem,selMenu,temp;
- Str255 name;
- GrafPtr tempPort;
- MenuHandle theMenu;
-
- selItem = LoWord(mResult);
- selMenu = HiWord(mResult);
- switch (selMenu) {
- case kAppleMenu:
- if (selItem>2) {
- GetPort(&tempPort);
- SetCursor(&qd.arrow);
- theMenu = GetMHandle(kAppleMenu);
- GetItem(theMenu,selItem,name);
- temp = OpenDeskAcc(name);
- SetPort(tempPort);
- }
- else CommAbout();
- break;
- case kFileMenu:
- switch (selItem) {
- case kNewItem:
- CommNew();
- break;
- case kOpenItem:
- CommOpen();
- break;
- case kCloseItem:
- CommCloseWindow(MyFrontWindow());
- break;
- case kSaveItem:
- CommSaveFile(MyFrontWindow());
- break;
- case kSaveAsItem:
- CommSaveAsFile(MyFrontWindow());
- break;
- case kPageSetupItem:
- CommPageSetup(MyFrontWindow());
- break;
- case kPrintItem:
- CommPrint(MyFrontWindow());
- break;
- case kQuitItem:
- gDone = true;
- break;
- }
- break;
- case kEditMenu:
- switch (selItem) {
- case kPrefsItem:
- CommEditPreferences();
- break;
- default:
- if (selItem>kClearItem)
- CommEdit(MyFrontWindow(),selItem);
- else if (!(SystemEdit(selItem-1)))
- CommEdit(MyFrontWindow(),selItem);
- break;
- }
- break;
- case kShapesMenu:
- switch (selItem) {
- case kGroupItem:
- SendWindowMessage(MyFrontWindow(),kGroupMessage,nil);
- break;
- case kUngroupItem:
- SendWindowMessage(MyFrontWindow(),kUnGroupMessage,nil);
- break;
- default:
- theMenu = GetMHandle(kShapesMenu);
- CheckItem(theMenu,gCurrentShape,false);
- CheckItem(theMenu,selItem,true);
- gCurrentShape = selItem;
- break;
- }
- break;
- case kMailMenu:
- switch (selItem) {
- case kSendItem:
- CommSendLetter(MyFrontWindow());
- break;
- case kAddRemMailItem:
- CommAddRemoveMailer(MyFrontWindow());
- break;
- case kReplyItem:
- CommReply(MyFrontWindow(),false);
- break;
- case kReplyToAllItem:
- CommReply(MyFrontWindow(),true);
- break;
- case kForwardItem:
- CommForward(MyFrontWindow());
- break;
- case kOpenNextItem:
- CommAdjacentLetter();
- break;
- case kTagLetterItem:
- CommTagLetter();
- break;
- }
- break;
- case kSignMenu:
- switch (selItem) {
- case kSignItem:
- CommSign(MyFrontWindow());
- break;
- case kVerifyItem:
- CommVerify(MyFrontWindow());
- break;
- case kShowSignItem:
- CommShowSigners(MyFrontWindow());
- break;
- }
- break;
- }
- HiliteMenu(0);
- }
-
-
- /* handles odoc appleevent */
-
- OSErr HandleOpenDoc(Boolean diskForm, FSSpec *fSpec, LetterSpec *lSpec)
- {
- WindowPtr window;
-
- return LoOpen(diskForm, fSpec, lSpec,true,&window);
- }
-
-
- /* handles pdoc appleevent */
-
- OSErr HandlePrintDoc(Boolean diskForm, FSSpec *fSpec, LetterSpec *lSpec)
- {
- OSErr err;
- WindowPtr window;
-
- err = LoOpen(diskForm,fSpec,lSpec,false,&window);
- if (err==noErr && window) {
- SendWindowMessage(window,kPrintMessage,nil);
- CommCloseWindow(window);
- }
- return noErr;
- }
-
-
- /* called when the program is about to exit- closes all of our open windows */
-
- Boolean ExitProgram(void)
- {
- WindowPtr window;
-
- while ((window=MyFrontWindow()) && gDone)
- CommCloseWindow(window);
-
- return gDone;
- }
-
-